package com.mogujie.tt.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.mogujie.tt.DB.entity.UserEntity;
import com.mogujie.tt.DB.sp.GesturespasswordSp;
import com.mogujie.tt.DB.sp.LoginSp;
import com.mogujie.tt.R;
import com.mogujie.tt.app.IMApplication;
import com.mogujie.tt.imservice.manager.IMLoginManager;
import com.mogujie.tt.imservice.service.IMService;
import com.mogujie.tt.imservice.support.IMServiceConnector;
import com.mogujie.tt.ui.widget.IMBaseImageView;

public class DigitalCodeActivity extends Activity implements OnClickListener{
	
	private ImageView[] viewy=new ImageView[6];
	private View[] views=new View[11];
	private String code="";
	private TextView delete,forgetpwd;
	private int i=0;

	private IMServiceConnector imServiceConnector = new IMServiceConnector(){
		@Override
		public void onServiceDisconnected() {}

		@Override
		public void onIMServiceConnected() {
			IMService imService = imServiceConnector.getIMService();
			if (imService == null) {
				return;
			}
			if (!imService.getContactManager().isUserDataReady()) {
				logger.i("detail#contact data are not ready");
			} else {
				init(imService);
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		imServiceConnector.connect(this);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.digitalcode_activity);
		findView();
		OnclkView();
	}
	

	private void findView() {
		// TODO Auto-generated method stub
		viewy[0] = (ImageView) findViewById(R.id.imageView1);
		viewy[1] = (ImageView) findViewById(R.id.imageView2);
		viewy[2] = (ImageView) findViewById(R.id.imageView3);
		viewy[3] = (ImageView) findViewById(R.id.imageView4);
		viewy[4] = (ImageView) findViewById(R.id.imageView5);
		viewy[5] = (ImageView) findViewById(R.id.imageView6);
		views[0] = findViewById(R.id.Rl0);
		views[1] = findViewById(R.id.Rl1);
		views[2] = findViewById(R.id.Rl2);
		views[3] = findViewById(R.id.Rl3);
		views[4] = findViewById(R.id.Rl4);
		views[5] = findViewById(R.id.Rl5);
		views[6] = findViewById(R.id.Rl6);
		views[7] = findViewById(R.id.Rl7);
		views[8] = findViewById(R.id.Rl8);
		views[9] = findViewById(R.id.Rl9);
		views[10] = findViewById(R.id.Ll);
		delete = (TextView) findViewById(R.id.textView2);
		forgetpwd = (TextView) findViewById(R.id.textView1);
	}

	private void init(IMService imService) {

		if (imService == null) {
			return;
		}
		final UserEntity loginContact = imService.getLoginManager().getLoginInfo();
		if (loginContact == null) {
			return;
		}
		IMBaseImageView portraitImageView = (IMBaseImageView)findViewById(R.id.user_portrait);
		portraitImageView.setDefaultImageRes(R.drawable.tt_default_user_portrait_corner);
		portraitImageView.setCorner(15);
		portraitImageView.setImageResource(R.drawable.tt_default_user_portrait_corner);
		portraitImageView.setImageUrl(loginContact.getAvatar());
	}

	private void OnclkView() {
		// TODO Auto-generated method stub
		for(int i=0;i<10;i++)
		views[i].setOnClickListener(this);
		delete.setOnClickListener(this);
		forgetpwd.setOnClickListener(this);
	}


	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.Rl0:
			dispose("0");
			break;
		case R.id.Rl1:
			dispose("1");
			break;
		case R.id.Rl2:
			dispose("2");
			break;
		case R.id.Rl3:
			dispose("3");
			break;
		case R.id.Rl4:
			dispose("4");
			break;
		case R.id.Rl5:
			dispose("5");
			break;
		case R.id.Rl6:
			dispose("6");
			break;
		case R.id.Rl7:
			dispose("7");
			break;
		case R.id.Rl8:
			dispose("8");
			break;
		case R.id.Rl9:
			dispose("9");
			break;
		case R.id.textView1:
			IMApplication.getInstance().getLockPatternUtils().Lockdelete();
			GesturespasswordSp.set(DigitalCodeActivity.this, GesturespasswordSp.getNUM(), "");
			GesturespasswordSp.set(DigitalCodeActivity.this, GesturespasswordSp.DELETE_LOCK, LoginSp.instance().getLockLoginId() + "Ttrue");
			IMLoginManager.instance().setKickout(false);
			IMLoginManager.instance().logOut();
			finish();
			break;
		case R.id.textView2:
			code="";
			for(int i=0;i<viewy.length;i++){
				viewy[i].setImageResource(R.drawable.dot_blur);
			}
			break;

		default:
			break;
		}
	}


	private void dispose(String c) {
		// TODO Auto-generated method stub
		
		code=code+c;
		System.out.println("1111111111="+code+"    ");
		if(code.length()<=viewy.length){
		for(int i=0;i<code.length();i++){
			viewy[i].setImageResource(R.drawable.dot_focus);
		}
		}
		if(code.length()==viewy.length){
			String dela = GesturespasswordSp.get(this, GesturespasswordSp.getNUM(), "");
			if(dela==""){
				return;
			}else{
				if(dela.equals(code)){
					finish();
				}else{
					i=i+1;
					for(int i=0;i<viewy.length;i++){
						viewy[i].setImageResource(R.drawable.dot_blur);
					}
					TranslateAnimation animation = new TranslateAnimation(10, -10, 0, 0);  
			        animation.setInterpolator(new OvershootInterpolator());  
			        animation.setDuration(100);  
			        animation.setRepeatCount(4);  
			        animation.setRepeatMode(Animation.REVERSE);  
			        views[10].startAnimation(animation);
			        Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);  
			        long [] pattern = {100,400};   // 停止 开启 停止 开启
			        vibrator.vibrate(pattern,-1); 
					code="";
					if (i==5){
						Toast.makeText(this,"密码输入5次，请重新登录",Toast.LENGTH_SHORT).show();
						IMApplication.getInstance().getLockPatternUtils().Lockdelete();
						GesturespasswordSp.set(DigitalCodeActivity.this, GesturespasswordSp.getNUM(), "");
						GesturespasswordSp.set(DigitalCodeActivity.this, GesturespasswordSp.DELETE_LOCK, LoginSp.instance().getLockLoginId() + "Ttrue");
						IMLoginManager.instance().setKickout(false);
						IMLoginManager.instance().logOut();
						finish();
					}
				}
			}

		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			moveTaskToBack(true);
			return true;
		}
		return super.onKeyDown(keyCode, event);

	}

}
